package com.example.lambda;

import org.junit.Test;

import java.util.Arrays;
import java.util.function.Function;

/**
 * compose and the
 *
 * @author qingyan 2022-03-09
 */
public class ComposeAndThen {

    @Test
    public void then(){
        Function<Integer, char[]> function = ((Function<Integer, Integer>)i -> i + i)
                .andThen(i -> i * i)
                .andThen(String::valueOf)
                .andThen(String::toCharArray);

        System.out.println(Arrays.toString(function.apply(10)));
    }

    @Test
    public void compose(){
        Function<Integer, Integer> compose = ((Function<Integer, Integer>)i -> {
            System.out.println(1);
            return i;
        }).compose((Function<Integer, Integer>)k -> {
            System.out.println(2);
            return k;
        }).compose((Function<Integer, Integer>)m -> {
            System.out.println(3);
            return m;
        });

        System.out.println(compose.apply(10));


    }


}
